package com.doubao.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.doubao.user.entity.TokenConsumptionRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * token消费记录Mapper接口
 */
@Mapper
public interface TokenConsumptionRecordMapper extends BaseMapper<TokenConsumptionRecord> {

    /**
     * 按日期范围查询消费统计
     *
     * @param userId 用户ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 每日消费统计
     */
    @Select("SELECT consumption_date as date, " +
            "SUM(prompt_tokens) as promptTokens, " +
            "SUM(completion_tokens) as completionTokens, " +
            "SUM(total_tokens) as totalTokens " +
            "FROM token_consumption_record " +
            "WHERE user_id = #{userId} " +
            "AND consumption_date BETWEEN #{startDate} AND #{endDate} " +
            "GROUP BY consumption_date " +
            "ORDER BY consumption_date")
    List<Map<String, Object>> selectConsumptionStatsByDateRange(@Param("userId") Long userId,
                                                                @Param("startDate") LocalDate startDate,
                                                                @Param("endDate") LocalDate endDate);

    /**
     * 按日期范围查询总消费量
     *
     * @param userId 用户ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 总消费量
     */
    @Select("SELECT COALESCE(SUM(total_tokens), 0) " +
            "FROM token_consumption_record " +
            "WHERE user_id = #{userId} " +
            "AND consumption_date BETWEEN #{startDate} AND #{endDate}")
    Integer selectTotalConsumptionByDateRange(@Param("userId") Long userId,
                                              @Param("startDate") LocalDate startDate,
                                              @Param("endDate") LocalDate endDate);
}